<template>
	<view class="usersetup">
		<view class="ys-li" @click="changeImg">
			<text>头像</text>
			<view class="name">
				<image :src="avatarUrl || '/static/image/default.svg'" mode="" class="avatar" @click.stop="preview">
				</image>
				<image src="/static/index/jiantou.png" mode=""></image>
			</view>
		</view>
		<navigator url="/pages/userinfo/index">
			<view class="ys-li">
				<text>昵称</text>
				<view class="name">
					{{info.nickname}}
					<image src="/static/index/jiantou.png" mode=""></image>
				</view>

			</view>
		</navigator>
		<view class="ys-li" @click="bindWx">
			<text>绑定公众号</text>
			<view class="name">
				{{info.has_gz?'已绑定':'未绑定'}}
				<image src="/static/index/jiantou.png" mode=""></image>
			</view>
		</view>
		<navigator url="/pages/user/certificate">
			<view class="ys-li">
				<text>证件上传</text>
				<view class="name">
					<image src="/static/index/jiantou.png" mode=""></image>
				</view>
			</view>
		</navigator>
		<navigator url="/pages/userpassword/index">
			<view class="ys-li">
				<text>登录密码</text>
				<image src="/static/index/jiantou.png" mode=""></image>
			</view>
		</navigator>
		<navigator url="/pages/userpasspay/index">
			<view class="ys-li">
				<text>支付密码</text>
				<image src="/static/index/jiantou.png" mode=""></image>
			</view>
		</navigator>
	</view>
</template>

<script>
	import {
		UserInfo,
		changeNickName,
		bindGz
	} from "@/api/api.js"
	export default {
		data() {
			return {
				info: [],
				avatarUrl: ''
			}
		},
		onLoad(e) {
			this.UserInfo()
			if (e.code) {
				bindGz({
					code: e.code
				}).then(res => {
					uni.$showMsg(res.msg)
					if (res.code == 0) {
						this.info.has_gz = true
					}
				})
			}
		},
		methods: {
			bindWx() {
				if (this.info.has_gz) {
					return uni.$showMsg('已经绑定过了')
				}
				let env = this.checkEnvironment()
				if (!env.isWeChat) {
					return uni.$showMsg('请在微信环境中授权')
				}
				// #ifdef H5
				const that = this;
				let redirect_uri = location.href.split('#')[0]
				window.location =
					`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${that.$config.WXAPPID}
					&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=abcdefghijklmnopqrstuvwxyz#wechat_redirect`;
				// #endif
			},
			UserInfo() {
				UserInfo().then(res => {
					if (res.code == 0) {
						this.info = res.data.user
						if (!res.data.user) {
							uni.switchTab({
								url: '/pages/user/index'
							})
							return false
						}
						this.name = this.info.nickname
						this.avatarUrl = res.data.user.avatar_path
					}
				})
			},
			preview() {
				uni.previewImage({
					urls: [this.avatarUrl]
				})
			},
			changeImg() {
				let that = this
				uni.chooseImage({
					sourceType: ['album', 'camera'], //从相册选择
					count: 1,
					success: (res) => {
						let igmFile = res.tempFilePaths;
						uni.showLoading({
							mask: true,
							title: '加载中'
						})
						uni.uploadFile({
							url: that.$config.requestUrl +
								'/common/upload',
							header: {
								'tokens': uni.getStorageSync('tokens')
							},
							formData: {
								accept: 'image',
								is_avatar: 1
							},
							filePath: igmFile[0],
							name: 'UploadFile',
							success: (res) => {
								uni.hideLoading()
								let imgRes = JSON.parse(res.data); //微信和头条支持
								if (imgRes.code == 0) {
									let imgData = imgRes.data
									that.avatarUrl = imgData.path
								} else {
									uni.$showMsg(imgRes.msg)
									return
								}
							},
							fail(err) {
								uni.hideLoading()
							}
						})
					},
				});
			},
		}
	}
</script>

<style lang="scss">
	.usersetup {
		padding-top: 60rpx;
	}

	.ys-li {
		font-size: 14px;
		color: #777;
		display: flex;
		background: #FFFFFF;
		justify-content: space-between;
		width: 86%;
		margin: 0 auto;
		border-bottom: 1px solid #efefef;
		padding: 40rpx;

		image {
			width: 30rpx;
			height: 30rpx;
		}

		.avatar {
			width: 50px;
			height: 50px;
			border-radius: 4px;
			position: relative;
			z-index: 1024;
		}

		.name {
			display: flex;
			align-items: center;

		}
	}
</style>